﻿using System;
using System.Collections.Generic;
using System.Web;
using WAD.WEB.AppCode;
using WAD.DAL;
using WAD.Model;

namespace WAD.WEB.Client.Ajax
{
    /// <summary>
    /// 发财：我的设置接口
    /// </summary>
    public class FC_Setting : AjaxBase
    {
        public override void AjaxMethodDispatch(String identity)
        {
            switch (identity)
            {
                case "get": this.Get(); break;
                case "setting": this.Setting(); break;
                case "getsharedbalance": this.GetSharedBalance(); break;
                case "updatelocation": this.UpdateLocation(); break;
            }
        }
        /// <summary>
        /// 分享主：获取我的设置
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void Get()
        {
            IsLogin();
            int user_id = Parse.Int("user_id", 0);
            string field = Parse.String("field", "");
            if (user_id < 1)
            {
                ParamError();
            }

            if (field.Equals("ali_name"))
            {

            }
            else
            {
                field = "";
            }

            Dictionary<string, object> model = DAOFCSetting.Get("fc_setting", "user_id=" + user_id, field);
            if (model == null)
            {
                DataEmpty();
            }
            Success(model);
        }

        /// <summary>
        /// 分享主: 更新我的设置
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void Setting()
        {
            IsLogin();
            MFCSetting m = new MFCSetting()
            {
                user_id = Parse.Int("user_id", 0),
                truename = Parse.String("truename", ""),
                mobile = Parse.String("mobile", ""),
                wx_name = Parse.String("wx_name", ""),
                wx_number = Parse.String("wx_number", ""),
                wx_picurl = Parse.String("wx_picurl", ""),
                ali_name = Parse.String("ali_name", ""),
                ali_truename = Parse.String("ali_truename", ""),
                area1 = Parse.String("area1", ""),
                area2 = Parse.String("area2", ""),
                area3 = Parse.String("area3", ""),
                date = DateTime.Now
            };

            if (m.user_id < 1) ParamError();
            m.area1 = m.area1.Replace("省", "");
            if (m.area2.Length > 0 && m.area2.Substring(m.area2.Length - 1).Equals("市"))
                m.area2 = m.area2.Replace("市", "");

            object old = DAOFCSetting.Exists("user_id=" + m.user_id);
            if (old == null)
            {
                if (m.wx_number == "")
                    m.wx_number = "0";
                DAOFCSetting.Add(m);        //第一次 设置需要添加一条记录
            }
            else
                DAOFCSetting.UpdateInfo(m); //第N次 设置只更新即可
            Success();
        }

        /// <summary>
        /// 分享主： 获取余额
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void GetSharedBalance()
        {
            IsLogin();
            int user_id = Parse.Int("user_id", 0);
            Dictionary<string, object> model = DaoUser.GetDetail(user_id, "id,shared_balance");
            model["shared_balance"] = DaoUser.BalanceDncode(model["shared_balance"], WJXConfig.EncryptConfig.DES_KEY);
            Success(model);
        }
        /// <summary>
        /// 分享主：更新区域
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void UpdateLocation()
        {
            int user_id = Parse.Int("user_id", 0);
            string area1 = Parse.String("area1", "").Replace("省", "").Replace("市","");
            string area2 = Parse.String("area2", "");
            string area3 = Parse.String("area3", "");

            if (area2.LastIndexOf("市", (area2.Length - 2)) > -1)
            {
                area2 = area2.Replace("市", "");
            }

            int count = DAOFCSetting.UpdateLocation(user_id, area1, area2, area3);
            if (count > 0)
                Success();
            Fail();
        }
    }
}